let isRegister = true;
$(function () {
    let userInfo = localStorage.getItem("userInfo");
    let msg = JSON.parse(userInfo);
    if (userInfo != null && msg.account != null && msg.account != undefined) {
        initData();
    }
});

function loginPage() {
    location.href = "/login.html";
}

function copyAccountNumber() {
    let val = document.getElementById("accountNumber").innerText; // 要复制的内容
    document.addEventListener('copy', save); // 监听浏览器copy事件
    document.execCommand('copy'); // 执行copy事件，这时监听函数会执行save函数。
    document.removeEventListener('copy', save); // 移除copy事件

    // 保存方法
    function save(e) {
        e.clipboardData.setData('text/plain', val); // 剪贴板内容设置
        e.preventDefault();
    }
    layer.msg("Copy to your clipboard successfully!",{
        icon:1
    });
}

function initData() {
    isRegister = false;
    let userInfo = localStorage.getItem("userInfo");
    let msg = JSON.parse(userInfo);
    $("#userId").val(msg.account);
    $("#userId").attr("disabled", "");
    $("#name").val(msg.account);
    $("#organization").val(msg.organization);
    $("#position").val(msg.position);
    $("#phone").val(msg.phone);
    $("#email").val(msg.email);
    $("#receiptType").val(msg.receiptType);
    $("#taxNum").val(msg.taxNumber);
    $("#receiptTitle").val(msg.receiptTitle);
    $("#registerBtn").html("Edit and Submit");
    $("#regTitle").html("View your registration information");
    $("#littleTitle").html("You can view and edit your registration information");
    $("#littleMsg").html("");
}

function register() {
    let account = $("#userId").val();
    let password = $("#password").val();
    let passwordConfirm = $("#passwordConfirm").val();
    let name = $("#name").val();
    let organization = $("#organization").val();
    let position = $("#position").val();
    let phone = $("#phone").val();
    let email = $("#email").val();
    let taxNum = $("#taxNum").val();
    let receiptTitle = $("#receiptTitle").val();
    let receiptType = $("#receiptType").val();
    if ($.trim(account).length == 0) {
        scrollToDiv("#userId");
        layer.tips('Please Enter Your Account！', '#userId', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }
    if (!commonClass.validNumAndEn(account, 4, 20)) {
        scrollToDiv("#userId");
        layer.tips('The account can only be composed of English letters and numbers, and its length must be between 4 and 20',
            '#userId', {
                tips: [1, '#ff0000'],
                time: 4000
            });
        return;
    }
    if ($.trim(password).length == 0) {
        scrollToDiv("#password");
        layer.tips('Please enter your password！', '#password', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }
    if (password.length < 6) {
        scrollToDiv("#password");
        layer.tips('Password length is at least 6 digits！', '#password', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }

    if ($.trim(passwordConfirm).length == 0) {
        scrollToDiv("#passwordConfirm");
        layer.tips('Please confirm your password！', '#passwordConfirm', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }

    if (password != passwordConfirm) {
        scrollToDiv("#passwordConfirm");
        layer.tips('The password you entered twice is inconsistent！', '#passwordConfirm', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }

    if ($.trim(name).length == 0) {
        scrollToDiv("#name");
        layer.tips('Please enter your name', '#name', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }

    if ($.trim(organization).length == 0) {
        scrollToDiv("#organization");
        layer.tips('Please enter your organization', '#organization', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }
    if ($.trim(position).length == 0) {
        layer.tips('Please enter your position', '#position', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }

    if ($.trim(phone).length != 0 && commonClass.validMobile(phone)) {
        layer.tips('Your mobile phone number is not in the correct format', '#phone', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }
    if ($.trim(email).length == 0) {
        layer.tips('Please enter your email', '#email', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }

    if (!commonClass.validEmail(email)) {
        layer.tips('Your email is not in the correct format', '#email', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }

    if($.trim(phone).length == 0){
        layer.tips('Please enter your phone number', '#phone', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }

    if(!commonClass.validNumAndEn(phone,5,12)){
        layer.tips('Your phone number is not in the correct format', '#phone', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }

    if($.trim(receiptTitle).length != 0){

        if ($.trim(taxNum).length == 0){
            layer.tips('You have to enter you tax number', '#taxNum', {
                tips: [1, '#ff0000'],
                time: 4000
            });
            return;
        }

        if(receiptType.length == 0){
            layer.tips('You have to provide you receipt type', '#receiptType', {
                tips: [1, '#ff0000'],
                time: 4000
            });
            return;
        }

    }

    let url = isRegister ? "/register" : "/update";
    let data = JSON.stringify({
        "account": account,
        "password": md5(password),
        "receiptTitle": receiptTitle,
        "phone": phone,
        "name": name,
        "organization": organization,
        "position": position,
        "email": email,
        "taxNumber": taxNum,
        "receiptType": receiptType
    });

    if (!isRegister) {
        data.password = null;
    }
    $.ajax({
        url: url,
        type: "post",
        contentType: "application/json",
        data: data,
        success: function (response) {
            if (response.code == 200) {
                localStorage.setItem("userInfo",data);
                if (isRegister) {
                    layer.msg("Sign Up Successfully!", {
                        icon: 1
                    });
                    $("#msgModal").modal('show');
                    commonClass.countDown(10, 'ok',"OK");
                    return;
                }

                $("#registerBtn").attr("disabled", "");

                layer.alert("update your message successfully!we will jump to the home page", {
                    skin: 'layui-layer-molv' //样式类名
                    , closeBtn: 0
                }, function () {
                    location.href = "/index.html";
                });

            } else if (response.code == 401) {
                localStorage.removeItem("userInfo");
                layer.alert(response.message, {
                    skin: 'layui-layer-molv' //样式类名
                    , closeBtn: 0
                }, function () {
                    location.href = "/login.html";
                });
            } else {
                layer.msg(response.message);
            }
        },
        complete: function () {
            layer.closeAll('loading');
        },
        error: function () {
            layer.msg("Your network may be disconnected!");
        },
        beforeSend: function () {
            showProgress();
        }
    });

}

function showProgress() {
    layer.load(0, {
        shade: [0.5, '#fff'] //0.1透明度的白色背景
    });
}

function scrollToDiv(dom) {
    $('html, body').animate({
        scrollTop: $(dom).offset().top - 200
    }, 500);
}